package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.Freeze;
import com.itheima.mapper.FreezeMapper;
import com.itheima.service.db.FreezeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class FreezeServiceImpl implements FreezeService {

    @Autowired
    private FreezeMapper freezeMapper;
    @Override
    public void save(Freeze freeze) {
        freezeMapper.insert(freeze);
    }

    /**
     * 解冻
     * @param unfreeze
     */
    @Override
    public void update(Freeze unfreeze) {
        LambdaQueryWrapper<Freeze> qw = new LambdaQueryWrapper<>();
        qw.eq(Freeze::getUserId,unfreeze.getUserId());
        freezeMapper.update(unfreeze,qw);
    }

    /**
     * 查询所以状态不正常的数据,排除永久冻结
     *
     * @return
     */
    @Override
    public List<Freeze> findAllFreeze() {
        LambdaQueryWrapper<Freeze> qw = new LambdaQueryWrapper<>();
        qw.ne(Freeze::getFreezingTime,3).eq(Freeze::getDeleted,0);
        return freezeMapper.selectList(qw);
    }

    @Override
    public Freeze findByUserId(Long userId) {
        LambdaQueryWrapper<Freeze> qw = new LambdaQueryWrapper<>();
        qw.eq(Freeze::getUserId,userId).eq(Freeze::getDeleted,0);
        return freezeMapper.selectOne(qw);
    }
}
